var a
var page = 1
$.ajax({
    type: "GET",
    url: "/getxqss",
    data: {
        page: 1,
        limit: 2
    },
    success: (data) => {
        const obj = data.data.rows
        a = Math.ceil((data.data.count) / 2)
        for (let i = 0; i < obj.length; i++) {
            $(".mainbar").append(`
            <img class="xqssimg" src="${obj[i].img}">
            <div class="articlexqss">
            <h3>${obj[i].text}...</h3>
            <p>发布时间：${obj[i].data}</p>
            <p>点赞：<a id="dz${obj[i].id}">${obj[i].dz}</a></p>
          </div>
          `)
        }
        $(".mainbar").append(
            `
            <div class="fenye111">
            <li><img src="../images/left.png" class="fenyeleft"></li>
            <input tpye="text" class="fenyesousuo">
            <button class="buttonfenye">go</button>
            </div> 
        `)
        for (let i = 0; i < a; i++) {
            $(".fenye111").append(`<li class="fenyeli${i+1}">${i+1}</li>`)
        }
        $(".fenye111").append(` <li><img src="../images/right.png" class="fenyeright"></li>`)
    }
})

$.ajax({
    type: "GET",
    url: "/getboke/getReading",
    success: (data) => {
        const obj = data.data
        for (let i = 0; i < obj.length; i++) {
            $(".ex_menu").append(`
            <li class="cebianli"><a href="/html/xq.html?id=${obj[i].id}">${obj[i].Boketit}</br>${obj[i].Reading}阅读量</a><br />
            </li>
          `)
        }
    }
})
$(".mainbar").click((e) => {
    if (e.target.tagName == "LI") {
        page = $(e.target).text()
        $.ajax({
            type: "GET",
            url: "/getxqss",
            data: {
                page,
                limit: 2
            },
            success: (data) => {
                $(".mainbar").html("")
                const obj = data.data.rows
                for (let i = 0; i < obj.length; i++) {
                    $(".mainbar").append(`
                <img class="xqssimg" src="${obj[i].img}">
            <div class="articlexqss">
            <h3>${obj[i].text}...</h3>
            <p>发布时间：${obj[i].data}</p>
            <p>点赞：<a id="dz${obj[i].id}">${obj[i].dz}</a></p>
              </div>
              `)
                }
                $(".mainbar").append(
                    `
            <div class="fenye111">
            <li><img src="../images/left.png" class="fenyeleft"></li>
            <input tpye="text" class="fenyesousuo">
            <button class="buttonfenye">go</button>
            </div> 
            `)
                for (let i = 0; i < a; i++) {
                    $(".fenye111").append(`<li class="fenyeli${i+1}">${i+1}</li>`)
                }
                $(".fenye111").append(` <li><img src="../images/right.png" class="fenyeright"></li>`)
            }
        })
    } else if (e.target.tagName == "BUTTON") {
        page = $(".fenyesousuo").val()
        $.ajax({
            type: "GET",
            url: "/getxqss",
            data: {
                page,
                limit: 2
            },
            success: (data) => {
                $(".mainbar").html("")
                const obj = data.data.rows
                for (let i = 0; i < obj.length; i++) {
                    $(".mainbar").append(`
            <img class="xqssimg" src="${obj[i].img}">
            <div class="articlexqss">
            <h3>${obj[i].text}...</h3>
            <p>发布时间：${obj[i].data}</p>
            <p>点赞：<a id="dz${obj[i].id}">${obj[i].dz}</a></p>
          </div>
          `)
                }
                $(".mainbar").append(
                    `
        <div class="fenye111">
        <li><img src="../images/left.png" class="fenyeleft"></li>
        <input tpye="text" class="fenyesousuo">
        <button class="buttonfenye">go</button>
        </div> 
        `)
                for (let i = 0; i < a; i++) {
                    $(".fenye111").append(`<li class="fenyeli${i+1}">${i+1}</li>`)
                }
                $(".fenye111").append(` <li><img src="../images/right.png" class="fenyeright"></li>`)
            }
        })
    } else if ($(e.target).attr("class") == "fenyeleft") {
        if (page > 1) {
            page = page - 1
            $.ajax({
                type: "GET",
                url: "/getxqss",
                data: {
                    page,
                    limit: 2
                },
                success: (data) => {
                    $(".mainbar").html("")
                    const obj = data.data.rows
                    for (let i = 0; i < obj.length; i++) {
                        $(".mainbar").append(`
            <img class="xqssimg" src="${obj[i].img}">
            <div class="articlexqss">
            <h3>${obj[i].text}...</h3>
            <p>发布时间：${obj[i].data}</p>
            <p>点赞：<a id="dz${obj[i].id}">${obj[i].dz}</a></p>
          </div>
          `)
                    }
                    $(".mainbar").append(
                        `
            <div class="fenye111">
             <li><img src="../images/left.png" class="fenyeleft"></li>
            <input tpye="text" class="fenyesousuo">
            <button  class="buttonfenye">go</button>
            </div> 
            `)
                    for (let i = 0; i < a; i++) {
                        $(".fenye111").append(`<li class="fenyeli${i+1}">${i+1}</li>`)
                    }
                    $(".fenye111").append(` <li><img src="../images/right.png" class="fenyeright"></li>`)
                }
            })
        }
    } else if ($(e.target).attr("class") == "fenyeright") {
        if (page < a) {
            page = page + 1
            $.ajax({
                type: "GET",
                url: "/getxqss",
                data: {
                    page,
                    limit: 2
                },
                success: (data) => {
                    $(".mainbar").html("")
                    const obj = data.data.rows
                    for (let i = 0; i < obj.length; i++) {
                        $(".mainbar").append(`
                    <img class="xqssimg" src="${obj[i].img}">
                    <div class="articlexqss">
                    <h3>${obj[i].text}...</h3>
                    <p>发布时间：${obj[i].data}</p>
                    <p>点赞：<a id="dz${obj[i].id}">${obj[i].dz}</a></p>
                  </div>
                  `)
                    }
                    $(".mainbar").append(
                        `
            <div class="fenye111">
             <li><img src="../images/left.png" class="fenyeleft"></li>
            <input tpye="text" class="fenyesousuo">
            <button  class="buttonfenye">go</button>
            </div> 
            `)
                    for (let i = 0; i < a; i++) {
                        $(".fenye111").append(`<li class="fenyeli${i+1}">${i+1}</li>`)
                    }
                    $(".fenye111").append(` <li><img src="../images/right.png" class="fenyeright"></li>`)
                }
            })
        }
    }
})
var arr = []
//点赞事件
$(".mainbar").click((e) => {
    if (e.target.tagName == "A") {
        $.ajax({
            type: "GET",
            url: "/getxqss/getdz",
            data: {
                id: $(e.target).attr("id").slice(-1)
            },
            success: (data) => {
                const id = data.data.rows[0].id
                const dz = data.data.rows[0].dz
                $.ajax({
                    type: "POST",
                    url: "/api/admin/find",
                    data: {
                        loginId: document.cookie.slice(6)
                    },
                    success: (data) => {
                        str = Array((data.data.dzg).toString().split(","))
                        arr=str[0]
                        if (str[0].includes($(e.target).attr("id").slice(-1)) == false) {
                            $.ajax({
                                type: "GET",
                                url: "/getxqss/updatebk",
                                data: {
                                    id: id,
                                    dz: Number(dz) + 1
                                },
                                success: (data) => {
                                    if (data.data[0] == 1) {
                                        // location.href = location.href
                                    }
                                    arr.push($(e.target).attr("id").slice(-1))
                                    var adminid = document.cookie.slice(6)
                                    $.ajax({
                                        type: "GET",
                                        url: "/api/admin/updateadmin",
                                        data: {
                                            id: adminid,
                                            dzg: arr.toString()
                                        },
                                        success: (data) => {}
                                    })
                                }
                            })
                        }
                    }
                })
            }
        })
    }
})